﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using DataEntity;
using Model;

namespace SWA_View
{
    public partial class Resistance : Form
    {
        int _ProjectID = 0;
        public Resistance(int ProjectID)
        {
            InitializeComponent();

            _ChiShuiEntitys.CollectionChanged += (sender1, e1) =>
            {
                listViewDraft.Items.Clear();
                ListViewItem lvi = null;
                for (int x = 0; x < _ChiShuiEntitys.Count; x++)
                {
                    lvi = new ListViewItem(_ChiShuiEntitys[x].ShouChiShui + "");
                    lvi.Tag = _ChiShuiEntitys[x];
                    //lvi.SubItems.Add((x + 1) + "");
                    lvi.SubItems.Add(_ChiShuiEntitys[x].WeiChiShui + "");
                    lvi.SubItems.Add(_ChiShuiEntitys[x].DSC);
                    this.listViewDraft.Items.Add(lvi);
                }
            };

            _SpeedEntitys.CollectionChanged += (sender2, e2) =>
            {
                listViewSpeed.Items.Clear();
                ListViewItem lvi = null;
                for (int x = 0; x < _SpeedEntitys.Count; x++)
                {
                    lvi = new ListViewItem(_SpeedEntitys[x].Speed + "");
                    lvi.Tag = _SpeedEntitys[x];

                    this.listViewSpeed.Items.Add(lvi);
                }
            };
            _ProjectID = ProjectID;
            CommonModel model = new CommonModel();


            foreach (var c in model.Query<ChiShuiEntity>("ProjectID=" + _ProjectID + " and ExperimentType ='" + ExperimentType.阻力实验 + "'"))
            {
                ChiShuiEntitys.Add(c);
            }
            foreach (var c in model.Query<SpeedEntity>("ProjectID=" + _ProjectID + " and ExperimentType ='" + ExperimentType.阻力实验 + "'"))
            {
                SpeedEntitys.Add(c);
            }

        }

        private ObservableCollection<ChiShuiEntity> _ChiShuiEntitys = new ObservableCollection<ChiShuiEntity>();
        public ObservableCollection<ChiShuiEntity> ChiShuiEntitys
        {
            get { return _ChiShuiEntitys; }
        }
        private ObservableCollection<SpeedEntity> _SpeedEntitys = new ObservableCollection<SpeedEntity>();
        public ObservableCollection<SpeedEntity> SpeedEntitys
        {
            get { return _SpeedEntitys; }
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {

            DialogResult rsltEnsure = MessageBox.Show("确定要退出吗？", "确认关闭", MessageBoxButtons.OKCancel);
            if (rsltEnsure == DialogResult.OK)
            {

                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
            else
            {

            }
        }



        private void Resistance_Load(object sender, EventArgs e)
        {

        }

        
       

        private float _speedCommon;
        public float SpeedCommon
        {
            get { return _speedCommon; }
            set { _speedCommon = value; }
        }

       
        private void btnDraft_Click(object sender, EventArgs e)
        {
            DraftForm draftDlg = new DraftForm(0, _ProjectID, ExperimentType.阻力实验);
            DialogResult rsltDraft = draftDlg.ShowDialog(this);
            if (rsltDraft == System.Windows.Forms.DialogResult.OK)
            {
                ChiShuiEntitys.Add(draftDlg.Entity);
            }
        }

        private void btnSpeed_Click(object sender, EventArgs e)
        {
            SpeedForm speedDlg = new SpeedForm(0, _ProjectID, ExperimentType.阻力实验);
            DialogResult rsltSpeed = speedDlg.ShowDialog(this);
            if (rsltSpeed == System.Windows.Forms.DialogResult.OK)
            {
                SpeedEntitys.Add(speedDlg.Entity);
            }
        }

        public ObservableCollection<ExperimentCombinationEntity> datas = null;
        private void btnOK_Click(object sender, EventArgs e)
        {
            CommonModel model = new CommonModel();
            if (DelSpeedID.Count > 0)
            {
                string condition = "ID in (";
                foreach (int i in DelSpeedID)
                {
                    condition += i + ", ";
                }
                condition = condition.Trim().TrimEnd(new char[] { ',' }) + ")";
                model.delete("SpeedEntity", condition);
            }

            if (DelDraftID.Count > 0)
            {
                string condition = "ID in (";
                foreach (int i in DelDraftID)
                {
                    condition += i + ", ";
                }
                condition = condition.Trim().TrimEnd(new char[] { ',' }) + ")";
                model.delete("ChiShuiEntity", condition);
            }

            if (listViewDraft.Items.Count == 0 || listViewSpeed.Items.Count == 0)
            {
                MessageBox.Show("请添加实验数据。");
                return;
            }
            datas = new ObservableCollection<ExperimentCombinationEntity>();
            foreach (var c in _ChiShuiEntitys)
            {
                foreach (var s in _SpeedEntitys)
                {
                    ExperimentCombinationEntity entity = null;
                    if (s.ID > 0 && c.ID > 0)
                    {
                        entity = model.Query<ExperimentCombinationEntity>("DataCombination = '" + c.Text + s.Text + "'").FirstOrDefault();
                    }
                    if (entity == null)
                    {
                        entity = new ExperimentCombinationEntity();
                        entity.DataCombination = c.Text + s.Text;
                        entity.CreateDate = DateTime.Now;
                        entity.IsFinished = "否";
                        entity.IsIng = "否";
                        entity.ProjectID = _ProjectID;
                        entity.ExperimentType = ExperimentType.阻力实验.ToString();
                    }
                    datas.Add(entity);
                }
            }

            foreach (var c in _ChiShuiEntitys)
            {
                if (c.ID <= 0)
                    model.Save<ChiShuiEntity>(c);
                else
                    model.Update<ChiShuiEntity>(c);
            }

            foreach (var s in _SpeedEntitys)
            {
                if (s.ID <= 0)
                    model.Save<SpeedEntity>(s);
                else
                    model.Update<SpeedEntity>(s);
            }

            this.DialogResult = DialogResult.OK;

        }

        private void btnDraftDel_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listViewDraft.SelectedItems)
            {
                ChiShuiEntity entity = item.Tag as ChiShuiEntity;
                if (entity != null && 0 != entity.ID)
                {
                    DelDraftID.Add(entity.ID);
                }
                _ChiShuiEntitys.Remove(entity);
            }
        }

        private List<int> DelSpeedID = new List<int>();

        private List<int> DelDraftID = new List<int>();

        private void btnSpeedDel_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listViewSpeed.SelectedItems)
            {
                SpeedEntity entity = item.Tag as SpeedEntity;
                if (entity != null && 0 != entity.ID)
                {
                    DelSpeedID.Add(entity.ID);
                }
                SpeedEntitys.Remove(entity);  
            }
        }

    }
}
